package com.qf.xiaomi.web.servlet;

import com.qf.xiaomi.pojo.*;
import com.qf.xiaomi.service.OrderService;
import com.qf.xiaomi.service.impl.AddressServiceImpl;
import com.qf.xiaomi.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 订单处理
 * @Created by 马英超 on 2022/8/17 8:01
 * @author： 马英超
 * @version：v1.0
 * @description:
 */
public class OrderServlet {

    //我的订单进入订单查询
    protected String show(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取用户
        final HttpSession httpSession = req.getSession();
       User user=(User) httpSession.getAttribute("user");
       if (user==null){
           return "redirect:/error/error.jsp";
       }
        final int userId = user.getId();
       OrderService orderService=new OrderServiceImpl();
       //根据用户名查询订单信息
        List<OrderList> ordersList= orderService.queryOrderByUserId(userId);
        if (ordersList!=null){
            httpSession.setAttribute("ordersList",ordersList);
            return "redirect:/orderList.jsp";
        }


        return "redirect:/error/error.jsp";


    }

    /**
     * 订单预览页面前置查询--查询用户地址
     * @param req
     * @param resp
     * @return
     */
    //订单预览内的地址查询
    private String  preView(HttpServletRequest req, HttpServletResponse resp){
        final HttpSession httpSession = req.getSession();
        //下单预览页面需要用户信息、商品信息、地址信息
        final User user =(User)httpSession.getAttribute("user");
        if (user==null)
            return "redirect:/error/error.jsp";
        final int userId = user.getId();
        //调用AddressService 查询地址
        AddressServiceImpl addressService=new AddressServiceImpl();
        List<TbAddress> addressList=addressService.queryAddressByUserId(userId);
        httpSession.setAttribute("addressList",addressList);
        return "redirect:/order.jsp";
    }

    /**
     * 创建订单
     * @param req
     * @param resp
     * @return
     */
    //创建订单
    private String  create(HttpServletRequest req, HttpServletResponse resp){
        final HttpSession httpSession = req.getSession();
        //下单预览页面需要用户信息、商品信息、地址信息
        final String aid = req.getParameter("aid");
        final User user =(User)httpSession.getAttribute("user");
        //参数处理
        if (user==null||user.getId()==0||aid==null||aid.length()==0){
            return "redirect:/error/error.jsp";
        }
        final int userId = user.getId();
        final int addressId=Integer.parseInt(aid);
        //从session中获取购物车数据并将其插入订单详情内
        final  List<CartUnion> cart = (List<CartUnion>) httpSession.getAttribute("cart");
       //如果购物车为空则定向到首页，否则将购物车信息插入到订单中间表内
        if(cart==null)
            return "redirect:/index.jsp";

   //调用OrderService将购物车内所有数据生成一个订单
        OrderService orderService=new OrderServiceImpl();
        final Map order = orderService.createOrder(userId, addressId, cart);
        if (order!=null){
            httpSession.setAttribute("order",order);
            return "redirect:/orderSuccess.jsp";
        }
        return "redirect:/error/error.jsp";
    }

    //订单详情查询
    private String detail(HttpServletRequest req, HttpServletResponse resp){
        final HttpSession httpSession = req.getSession();
        //获取请求参数
        final String oid = req.getParameter("oid");
        final User user = (User) httpSession.getAttribute("user");


        //参数处理
        if (user==null||oid.length()==0||oid==null){
            return "redirect:/error/error.jsp";
        }
        final int userId = user.getId();

        //调用Service
        OrderService orderService=new OrderServiceImpl();
        final OrderDetail orderDetail = orderService.queryOrderDetail(oid, userId);
        if (orderDetail!=null){
            httpSession.setAttribute("order",orderDetail);
            return "redirect:/orderDetail.jsp";
        }
        return "redirect:/error/error.jsp";
    }
}
